package com.jingyanzi.privilege.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.privilege.dao.SecurityRoletableMapper;
import com.jingyanzi.privilege.dao.SecurityTableMapper;

import com.jingyanzi.privilege.domain.FieldDO;
import com.jingyanzi.privilege.domain.TableDO;
import com.jingyanzi.privilege.domain.result.ResultDO;
import com.jingyanzi.privilege.domain.result.ResultSupport;
import com.jingyanzi.privilege.entity.SecurityRoletableExample;
import com.jingyanzi.privilege.entity.SecurityTable;

import com.jingyanzi.privilege.query.FieldQueryDO;
import com.jingyanzi.privilege.query.TableQueryDO;
import com.jingyanzi.privilege.service.IFieldService;
import com.jingyanzi.privilege.service.ITableService;
import com.jingyanzi.privilege.util.BeanUtilsExtends;

@Service("tableService")
public class TableServiceImpl extends BaseServiceImpl implements ITableService {

	@Autowired
	private SecurityTableMapper tableMapper;

	@Autowired
	private SecurityRoletableMapper roleTableMapper;

	@Autowired
	private IFieldService fieldService;

	public ResultDO add(TableDO tableDO) {

		SecurityTable table = new SecurityTable();

		ResultSupport result = BeanUtilsExtends.copy(table, tableDO);
		if (!result.isSuccess())
			return result;
		table.setCreatetime(Calendar.getInstance().getTime());

		int r = -1;
		try {
			r = tableMapper.insert(table);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加表信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("添加表信息失败");
			result.setSuccess(false);
			return result;
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, table.getId());
		return result;
	}

	public ResultDO remove(Integer id) {

		ResultSupport result = new ResultSupport();

		SecurityTable table = new SecurityTable();

		table.setId(id);
		table.setIsdelete(-1);

		int r = -1;

		r = removeMapping(id);

		if (r > -1) {
			r = -1;
			try {
				r = tableMapper.updateByPrimaryKeySelective(table);
			} catch (Exception e) {
				//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg("删除表信息失败");
				result.setSuccess(false);
				return result;
			}
		}

		if (r < 1) {
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("删除表信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO modifi(TableDO tableDO) {

		SecurityTable table = new SecurityTable();

		ResultSupport result = BeanUtilsExtends.copy(table, tableDO);
		if (!result.isSuccess())
			return result;
		table.setCreatetime(Calendar.getInstance().getTime());

		int r = -1;
		try {
			r = tableMapper.updateByPrimaryKeySelective(table);
		} catch (Exception e) {
			e.printStackTrace();
			//result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg("修改表信息失败");
			result.setSuccess(false);
			return result;
		}
		if (r < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	public ResultDO get(Integer id) {

		ResultSupport result = new ResultSupport();

		if (id < 1) {
			result.setSuccess(false);
			return result;
		}

		SecurityTable table = null;

		try {
			table = tableMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (table == null) {
			result.setSuccess(false);
		}
		FieldQueryDO query = new FieldQueryDO();
		query.setTableId(id);
		ResultDO res = fieldService.query(query);

		if (!res.isSuccess()) {
			res.setSuccess(false);
			return res;
		}
		TableDO tableDO = getTableDO(table);
		tableDO.setFileList((List<FieldDO>) res.getModel(ResultSupport.FIRST_MODEL_KEY));
		result.setModel(ResultSupport.FIRST_MODEL_KEY, getTableDO(table));
		return result;
	}

	public ResultDO query(TableQueryDO query) {

		return null;
	}

	private int removeMapping(int id) {

		int r = -1;

		if (r < -1) {
			return -1;
		}

		r = -1;

		SecurityRoletableExample example2 = new SecurityRoletableExample();

		example2.createCriteria().andTabileidEqualTo(id);
		try {
			r = roleTableMapper.deleteByExample(example2);
		} catch (Exception e) {
			return -1;
		}

		if (r < -1) {
			return -1;
		}

		return 1;
	}

}
